package cn.gok.server.controller;

import cn.gok.common.resp.Result;
import cn.gok.utils.ResultUtil;
import cn.gok.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: DxlinY
 * @apiNote: 错误统一返回接口
 * @date: 2020/9/25
 * @time: 14:07
 */
@ControllerAdvice
@Slf4j
public class ErrorControllerEx {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result globalExceptionHandler(Exception e) {
        log.error("接口出现异常{}", e.getMessage());
        e.printStackTrace();
        return ResultUtil.error(e.getMessage());
    }

    /**
     * 无权限访问
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public Result hasNoPermission(AuthorizationException e) {
        log.error("用户无权限访问{}", UserContext.user().getUserName());
        return ResultUtil.hasNotPermission();
    }
}
